package com.hbnu.service.impl;

import com.hbnu.mapper.GoodsMapper;
import com.hbnu.pojo.Category;
import com.hbnu.pojo.Good;
import com.hbnu.pojo.Request;
import com.hbnu.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<Category> getCategoryList(){
        return goodsMapper.getCategoryList();
    }

    public List<Good> getGoodListByCid(Request rq){
        return goodsMapper.getGoodListByCid(rq.getCid());
    }

    public List<Good> getGoodListByContent(Request rq){
        return goodsMapper.getGoodListByContent(rq.getContent());
    }

    @Override
    public List<Good> getSellingGoodListByCid(Request rq) {
        return goodsMapper.getGoodListByCidAndState(rq.getCid(),1);
    }

    @Override
    public List<Good> getSellingGoodListByContent(Request rq) {
        return goodsMapper.getGoodListByStateAndContent(rq.getContent(),1);
    }

    @Override
    public List<Good> getGoodListByState(Request rq) {
        return goodsMapper.getGoodListByState(rq.getState());
    }

    @Override
    public List<Good> getGoodListByContentStateCid(Request rq) {
        if(rq.getState()==0)
        {
            if(rq.getCid()==null)return goodsMapper.getGoodListByContent(rq.getContent());
            else return goodsMapper.getGoodListByCidAndContent(rq.getCid(),rq.getContent());
        }
        else{
            if(rq.getCid()==null)return goodsMapper.getGoodListByStateAndContent(rq.getContent(),rq.getState());
            else return goodsMapper.getGoodListByContentStateCid(rq.getCid(),rq.getContent(), rq.getState());
        }
    }

    public void addCategory(Request rq){
        List<Category>categoryList=goodsMapper.getCategoryList();
        String max_id=new String("000000000000");
        for(Category category:categoryList){
            if(max_id.compareTo(category.getCid())<0)
                max_id=category.getCid();
        }
        String cid=String.format("%0"+12+"d",Long.parseLong(max_id)+1);
        goodsMapper.insertCategory(cid,rq.getCname());
    }

    @Override
    public void addGood(Request rq) {
        List<Good>goodList=goodsMapper.getGoodList();
        String max_id=new String("0000000000000");
        for(Good good:goodList){
            if(max_id.compareTo(good.getGid())<0)
                max_id=good.getGid();
        }
        String gid=String.format("%0"+13+"d",Long.parseLong(max_id)+1);
        goodsMapper.insertGood(gid, rq.getGname(),rq.getCid(),rq.getPrice(),rq.getDesc(),rq.getSrc(),rq.getNum(), rq.getState());
    }

    @Override
    public void modifyGoodGname(Request rq) {
        goodsMapper.updateGoodGname(rq.getGname(),rq.getGid());
    }

    @Override
    public void modifyGoodDesc(Request rq) {
        goodsMapper.updateGoodDesc(rq.getDesc(),rq.getGid());
    }

    @Override
    public void modifyGoodNum(Request rq) {
        goodsMapper.updateGoodNum(rq.getNum(), rq.getGid());
    }

    @Override
    public void modifyGoodPrice(Request rq) {
        goodsMapper.updateGoodPrice(rq.getPrice(), rq.getGid());
    }

    @Override
    public void modifyGoodSrc(Request rq) {
        goodsMapper.updateGoodSrc(rq.getSrc(), rq.getGid());
    }

    @Override
    public void modifyGoodState(Request rq) {
        goodsMapper.updateGoodState(rq.getState(), rq.getGid());
    }

    @Override
    public void updateGoodInfo(Request rq) {
        modifyGoodDesc(rq);
        modifyGoodGname(rq);
        modifyGoodNum(rq);
        modifyGoodPrice(rq);
        modifyGoodSrc(rq);
        modifyGoodState(rq);
    }
}
